package fr.ssd.sdl.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import fr.ssd.sdl.config.Configurator;

public class JdbcHelper {

	private static Connection conn = null;
	private static PreparedStatement selectStatement;
	private static Statement insertStatement;

	public static PreparedStatement getSelectStatement() {
		return selectStatement;
	}

	public static Statement getInsertStatement() {
		return insertStatement;
	}
		
	static{
			try {
				Class.forName(Configurator.driver).newInstance();
				conn = DriverManager.getConnection(Configurator.url, Configurator.userName, Configurator.userPassword);
				selectStatement = conn.prepareStatement("select max(?) as max from ?");
				insertStatement = conn.createStatement();
			} catch (InstantiationException e) {
				System.err.println("Error during Jdbc driver instanciation");
				System.err.println(e.getMessage());
				System.exit(1);
			} catch (IllegalAccessException e) {
				System.err.println("Error during Jdbc driver instanciation");
				System.err.println(e.getMessage());
				System.exit(1);
			} catch (ClassNotFoundException e) {
				System.err.println(Configurator.driver + " class not found");
				System.exit(1);
			} catch (SQLException e) {
				System.err.println("Error during statement instantiation");
				System.err.println(e.getMessage());
				System.exit(1);
			}
			
		
	}

}
